﻿using System;
using Repository;
using Model.Domain_Model;
using Interfaces.Add_Interfaces;
using Interfaces.Information_Interfaces;
using Presenters.Inherited_Presenters;

namespace Presenters.Add_Presenters
{
    public class ClientAddPresenter:OperationStatus
    {
        public event EventHandler<EventArgs> ActionOnAnotherForm;
        private IClientAddView view = null;
        IDb database = Repo.GetDatabaseInstance();
        

        public ClientAddPresenter(IClientAddView view)
        {
            this.view = view;
        }

        private void Action()
        {
            if (ActionOnAnotherForm != null)
                ActionOnAnotherForm(this, EventArgs.Empty);
        }

        /// <summary>
        /// Zamyka okno dodawania klienta.
        /// </summary>
        public void ExitApp()
        {
            view.Close();
        }

        /// <summary>
        /// Zapisuje do bazy danych nowy obiekt typu Client
        /// </summary>
        public void Save()
        {
            nameOperation = "Dodawanie klienta";
            try
            {
                database.Clients.NewClient(new Client { idClient = 0, dateInsert = view.dateInsert, descriptionClient = view.descriptionClient, email = view.email, inventedName = view.inventedName, isProtected = view.isProtected, nameClient = view.nameClient, nameCorporation = view.nameCorporation, phoneNumber = view.phoneNumber, surnameClient = view.surnameClient });
                view.ShowSuccess();
                view.Close();
                Action();
            }
            catch (Exception ex)
            {
                exception = ex.Message;
                view.ShowError();
            }
        }
    }
}
